load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")

package(default_visibility = ["//rs:ic-os-pkg"])

exports_files([
    "templates/ic.json5.template",
])

DEPENDENCIES = [
    # Keep sorted.
    ":build_script",
    "//rs/config",
    "//rs/ic_os/config_types",
    "//rs/ic_os/deterministic_ips",
    "//rs/ic_os/network",
    "//rs/ic_os/sev",
    "//rs/ic_os/utils",
    "@crate_index//:anyhow",
    "@crate_index//:askama",
    "@crate_index//:clap",
    "@crate_index//:fs_extra",
    "@crate_index//:get_if_addrs",
    "@crate_index//:macaddr",
    "@crate_index//:regex",
    "@crate_index//:rust_ini",
    "@crate_index//:serde",
    "@crate_index//:serde_json",
    "@crate_index//:serde_with",
    "@crate_index//:tempfile",
    "@crate_index//:url",
    "@crate_index//:walkdir",
]

TEST_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:once_cell",
    "@crate_index//:tempfile",
]

MACRO_DEPENDENCIES = []

ALIASES = {}

cargo_build_script(
    name = "build_script",
    srcs = ["build.rs"],
    data = ["templates/ic.json5.template"],
)

BIN_SOURCES = [
    "src/main.rs",
]

LIB_SOURCES = glob(
    ["src/**/*.rs"],
    exclude = BIN_SOURCES,
)

rust_library(
    name = "config_lib",
    srcs = LIB_SOURCES,
    crate_name = "config",
    target_compatible_with = [
        "@platforms//os:linux",
    ],
    visibility = [
        "//rs:ic-os-pkg",
        "//rs:system-tests-pkg",
        "//rs/orchestrator:__pkg__",
    ],
    deps = DEPENDENCIES,
)

rust_library(
    name = "config_lib_dev",
    srcs = LIB_SOURCES,
    crate_features = ["dev"],
    crate_name = "config",
    target_compatible_with = [
        "@platforms//os:linux",
    ],
    visibility = [
        "//rs:ic-os-pkg",
        "//rs:system-tests-pkg",
    ],
    deps = DEPENDENCIES,
)

# You can update the golden files with
# bazel test //rs/ic_os/config:config_lib_test --test_env=UPDATE_GOLDENFILES=1 --spawn_strategy=local
rust_test(
    name = "config_lib_test_dev",
    crate = ":config_lib_dev",
    crate_features = ["dev"],
    env = {"CARGO_MANIFEST_DIR": "rs/ic_os/generate_guestos_vm_config"},
    deps = TEST_DEPENDENCIES,
)

rust_test(
    name = "config_lib_test",
    crate = ":config_lib",
    deps = TEST_DEPENDENCIES,
)

rust_binary(
    name = "config",
    srcs = BIN_SOURCES,
    aliases = ALIASES,
    crate_name = "config",
    proc_macro_deps = MACRO_DEPENDENCIES,
    target_compatible_with = [
        "@platforms//os:linux",
    ],
    deps = [
        ":config_lib",
    ] + DEPENDENCIES,
)

rust_binary(
    name = "config_dev",
    srcs = BIN_SOURCES,
    aliases = ALIASES,
    crate_features = ["dev"],
    crate_name = "config",
    proc_macro_deps = MACRO_DEPENDENCIES,
    target_compatible_with = [
        "@platforms//os:linux",
    ],
    deps = [
        ":config_lib_dev",
    ] + DEPENDENCIES,
)

rust_test(
    name = "config_test",
    crate = ":config_dev",
    crate_features = ["dev"],
    deps = TEST_DEPENDENCIES,
)
